<?php echo $this->render('/manage/header.phtml');?>
<style>
#title,#keywords,#coverimage{width:600px;}
</style>
<script type="text/javascript" charset="utf-8" src="<?php echo APPLICATION_PUBLIC; ?>/ueditor/editor_config.js"></script>
<script type="text/javascript" charset="utf-8" src="<?php echo APPLICATION_PUBLIC; ?>/ueditor/editor_all.js"></script>
<script type="text/javascript">
function addResourceDom()
{
	var domStr = '<div class="addResDom">' + 
	    '<select name="resType[]">' +
		'<option value="local">local</option>' +
		'<option value="remote">remote</option>' +
		'</select>' +
		'标题:<input type="text" name="resTitle[]"/>' +
		'地址:<input type="text" name="resAddress[]"/>' +
		'<a class="deleteRow" href="javascript:void(0);" title="Delete"><img src="/public/images/admin/icons/cross.png" alt="Delete"></a>' +
		'</div>';
	$("#addMusicVideoWrap").append(domStr);
}

$(function(){
	//实例化编辑器
    var ue = UE.getEditor('editor');
    
    $("#addMusicVideoBtn").click(function(){
    	var curType = $("#type").val();
    	if (curType == 5) {
    		addResourceDom();
    	} else if (curType == 6) {
        	if ($(".addResDom").length >= 1) {
            	alert('视频只能添加一个');
           	} else {
    			addResourceDom();
           	}
        } else {
            alert('当前的文章类型不能添加音乐或视频！');
        }
    });

    $(".deleteRow").live('click', function(){
		$(this).parent('div').remove();
    });
    //tags输入框的处理
    $("#tagsInput").keypress(function(e){
        if (e.keyCode == 44) {
            var str = '<span><input type="text" name="keywords[]" value="' + $(this).val() + '"/><a href="###" class="delTag">X</a></span>';
            $(this).val('');
            $("#tagsWrap").append(str);
            return false;
        }
    });
    //删除tag按钮
    $(".delTag").live("click", function(){
        $(this).parent().remove();
    });
});
</script>
</head>
<body>
<?php
	$tableInfo = Zend_Registry::get('database')->article;
	$authorInfo = Zend_Registry::get('database')->author;
?>
<div id="body-wrapper">
<?php echo $this->render('/manage/sidebar.phtml');?>
<div id="main-content">
    <h2>Edit Article</h2>
    <div class="clear"></div>
    <div class="content-box">
      <div class="content-box-header">
        <h3>Content box</h3>
        <div class="clear"></div>
      </div>
      <div class="content-box-content">
		<form action="<?php echo APPLICATION_URL; ?>/manage/save-article" method="post">
			<input type="hidden" id="id" name="id" value="<?php echo $this->article[$tableInfo->id];?>" />
			<p>文章标题：<input id="title" type="text" size="18" name="title" value="<?php echo $this->article[$tableInfo->title];?>" /></p>
			<p>标签(请用,分隔)：<span id="tagsWrap">
			<?php foreach ($this->article[$tableInfo->keywords] as $keyword) {?>
			<span><input type="text" name="keywords[]" value="<?php echo $keyword?>"/><a href="###" class="delTag">X</a></span>
			<?php }?>
			</span><input id="tagsInput" type="text" size="18" value="" /></p>
			<p>封面图片地址： <input id="coverimage" type="text" size="18" name="coverimage" value="<?php echo $this->article[$tableInfo->coverimage];?>" /></p>
			<p>作者：<select id="author" name="author">
			<?php
				foreach ($this->authors as $author) {
					if ($author[$authorInfo->userid] == $this->article[$tableInfo->author]) {
						$selected = 'selected="selected"';
					} else {
						$selected = '';
					}
			?>
				<option <?php echo $selected;?> value="<?php echo $author[$authorInfo->userid]?>"><?php echo $author[$authorInfo->author]?></option>
			<?php }?>
				</select>
			分类： <select id="type" name="type">
			<?php
				foreach ($this->categories as $key => $value) {
					if ($value == $this->article[$tableInfo->type]) {
						$selected = 'selected="selected"';
					} else {
						$selected = '';
					}
			?>
			<option value="<?php echo $value;?>" <?php echo $selected;?>><?php echo $key;?></option>
			<?php }?>
			</select>
			</p>
 			<div>
 				文章内容：
 				<div class="notification information png_bg"> <a href="#" class="close"><img src="<?php echo APPLICATION_PUBLIC; ?>/images/admin/icons/cross_grey_small.png" title="Close this notification" alt="close"></a>
					<div>请把issuu复制过来的embed code中的style改成:width:960px;height:500px;margin:0 auto;</div>
				</div>
 				<script  id="editor" type="text/plain" name="content">
					<?php echo $this->article[$tableInfo->content];?>
				</script>
 			</div>
 			
 			<div id="addMusicVideoWrap" style="margin-bottom: 10px;">
 				<p>添加音乐/视频<input id="addMusicVideoBtn" type="button" value="添加"/></p>
 				<?php 
 					$resources = json_decode($this->article[$tableInfo->url], true);
 					if (is_array($resources) && count($resources) > 0) {
 					foreach ($resources as $key => $value) {
						foreach ($value as $key2 => $val) {
							if ($key == 'local') {
								$title = $val['title'];
								$address = $val['url'];
							} else {
								$title = '';
								$address = $val;
							}
 				?>
 				<div class="addResDom">
				    <select name="resType[]">
					<option value="local" <?php if ($key == 'local') {echo 'selected="selected"';}?>>local</option>
					<option value="remote" <?php if ($key == 'remote') {echo 'selected="selected"';}?>>remote</option>
					</select>
					标题:<input type="text" name="resTitle[]" value='<?php echo $title;?>' />
					地址:<input type="text" name="resAddress[]" value='<?php echo $address;?>' />
					<a class="deleteRow" href="javascript:void(0);" title="Delete"><img src="/public/images/admin/icons/cross.png" alt="Delete"></a>
				</div>
				
				<?php }}} elseif (is_string($resources)) {?>
				<div class="addResDom">
				    <select name="resType[]">
					<option value="local">local</option>
					<option value="remote" selected="selected">remote</option>
					</select>
					标题:<input type="text" name="resTitle[]" value='' />
					地址:<input type="text" name="resAddress[]" value='<?php echo $resources;?>' />
					<a class="deleteRow" href="javascript:void(0);" title="Delete"><img src="/public/images/admin/icons/cross.png" alt="Delete"></a>
				</div>
				<?php }?>
 			</div>
 			
			2级页面描述: <br />
			<textarea id="description" style="width: 300px; height: 100px;" name="description"><?php echo $this->article[$tableInfo->description];?></textarea>
			<br />
			<input type="submit" value="Submit" />
			</form>
      </div>
    </div>
<?php echo $this->render('/manage/footer.phtml');?>
</div>
</div>
</body>
</html>